#include "StdAfx.h"
#include "ChunkItem.h"

using namespace Common;
using namespace ChunkSystem;

ChunkItem::LoadProgressListener::LoadProgressListener( ChunkItem* pTarget )
	:m_uProgressInterval(10),m_pTarget(pTarget)
{
}

ChunkItem::LoadProgressListener::~LoadProgressListener(void)
{
}

void ChunkItem::LoadProgressListener::SetProgressNotifyInterval( uint16 uInterval )
{
	m_uProgressInterval = uInterval;
}

uint16 ChunkItem::LoadProgressListener::GetProgressNotifyInterval( void )
{
	return m_uProgressInterval;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ChunkItem::ChunkItem( const String& strId, ChunkItem* pOwner )
	:m_strId(strId),m_matWorldTM(Matrix4::IDENTITY),m_aabb(AxisAlignedBox::BOX_INFINITE),m_pOwner(pOwner)
{
}

ChunkItem::~ChunkItem(void)
{
	m_conProgressListeners.clear();
}

bool ChunkItem::InitSync( void )
{
	return true;
}

Matrix4 ChunkItem::GetWorldTM(void)
{
	return m_matWorldTM;
}

void ChunkItem::Transform( const Common::Matrix4& rTransform )
{
	m_matWorldTM.concatenate( rTransform );
}

void ChunkItem::AttachLoadProgressListener( LoadProgressListener* pListener )
{
	if( pListener == NULL )
		return;

	if( std::find( m_conProgressListeners.begin(), m_conProgressListeners.end(), pListener ) != m_conProgressListeners.end() )
		return;

	m_conProgressListeners.push_back( pListener );
}

void ChunkItem::DettachLoadProgressListener( LoadProgressListener* pListener )
{
	if( pListener == NULL )
		return;
	ConProgressListeners::iterator pos = std::find( m_conProgressListeners.begin(), m_conProgressListeners.end(), pListener );
	if( pos == m_conProgressListeners.end() )
		return;

	m_conProgressListeners.erase( pos );
}

void ChunkItem::_OnLoad( uint16 uPercentage )
{
	ConProgressListeners::iterator pos = m_conProgressListeners.begin();
	for( ; pos != m_conProgressListeners.end(); pos++ )
	{
		//if( uPercentage % (*pos)->GetProgressNotifyInterval() != 0 )
			//continue;

		(*pos)->OnProgressIntervalNotify( uPercentage );
	}
}


/////////////////////////////////////////////////////////////////////////////////////////////////////////

ChunkItemFactory::ChunkItemFactory( void )
{
}

ChunkItemFactory::~ChunkItemFactory( void )
{
}

RenderSystem::Movable* ChunkItemFactory::Create( ChunkCreateContext* pContext )
{
	return this->_CreateImpl( pContext->pOwner, pContext->pChunkItemData );
}